package com.taptech.doufu.ui.view.topicview;

import android.app.Activity;
import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.LimitTimeGsonBean;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.ui.view.topicview.adapter.NovelTopicHorizontalAdapter;
import com.taptech.doufu.ui.view.countdownview.CustomCountDownTimer;
import com.taptech.doufu.ui.view.countdownview.DateUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicLimitTimeView extends LinearLayout{
    private TextView tvTitle;
    private String title;
    private String type;
    private String pageType;

    private List<DFHomeNovelBeans> list;
    public NovelTopicLimitTimeView(Context context) {
        super(context);
    }

    public NovelTopicLimitTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicLimitTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private View root_layout;
    private View linearLayout0;
    private View topSpacView;
    private Context context;
    private CustomCountDownTimer timer;
    private EventBean eventBean;
    private RecyclerView mRecyclerView;


    public NovelTopicLimitTimeView(final Context context, DFHomeNovelContentBeans model, final String type,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_limit_time_view, null);
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        root_layout = view.findViewById(R.id.root_layout);
        startTime(model.getEnd_time());
        tvTitle =  view.findViewById(R.id.tvTitle);
        tvTitle.setText(model.getTitle());
        title = model.getTitle();
        eventBean = model.getEventData();
        initContentView(view);
        changeDayModelBg();
        list = model.getList();
        setData(list);
        addView(view);
    }

    private void startTime(long endTime) {
        long time = (long) ((double) endTime*1000- System.currentTimeMillis());
        timer = new CustomCountDownTimer(time,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvHour.setText(DateUtils.INSTANCE.getHour(millisUntilFinished));
                tvMinute.setText(DateUtils.INSTANCE.getMinute(millisUntilFinished));
                tvSecond.setText(DateUtils.INSTANCE.getSecond(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                //拉取新数据
                getData("http://api.doufu.la/new_home/new_home/getTimeLimitFullbuys?sexuality=3");
            }
        };
        timer.start();

    }


    private void getData(String url) {
        ApiClient.getInstance().setUseCache(false).getService()
                .getLimitTimeData(url)
                .compose(RxJavaHelper.<LimitTimeGsonBean>observeOnMainThread((Activity) context))
                .subscribe(new BaseSubscriber<LimitTimeGsonBean>(){
                    @Override
                    public void onNext(LimitTimeGsonBean limitTimeGsonBean) {
                        super.onNext(limitTimeGsonBean);
                        if (limitTimeGsonBean != null && limitTimeGsonBean.getData() != null
                                && limitTimeGsonBean.getData().getList()!=null && limitTimeGsonBean.getData().getEnd_time()>0) {
                            startTime(limitTimeGsonBean.getData().getEnd_time());
                            setData(limitTimeGsonBean.getData().getList());
                        }else{
                            //hide
                            hideView();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        //隐藏
                        hideView();
                    }
                });
    }

    private void hideView() {
        llTime.setVisibility(GONE);
        tvOver.setVisibility(VISIBLE);
    }

    private TextView tvHour,tvMinute,tvSecond,tvSpitOne,tvSpitTwo,tvOver;
    private LinearLayout llTime;
    public void initContentView(View view){
        llTime = (LinearLayout) view.findViewById(R.id.llTime);
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        tvOver = (TextView) view.findViewById(R.id.tvOver);
        tvHour = (TextView) view.findViewById(R.id.tvHour);
        tvMinute = (TextView) view.findViewById(R.id.tvMinute);
        tvSecond = (TextView) view.findViewById(R.id.tvSecond);
        tvSpitOne = (TextView) view.findViewById(R.id.tvSpitOne);
        tvSpitTwo = (TextView) view.findViewById(R.id.tvSpitTwo);
    }

    private NovelTopicHorizontalAdapter adapter;
    private void setData(List<DFHomeNovelBeans> list){
        MeasuredLinearLayoutManager manager = new MeasuredLinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        manager.setScrollEnabled(true);
        adapter = new NovelTopicHorizontalAdapter(context, list, ContentType.PRICE,type,title,eventBean,pageType);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(adapter);
    }

    public void refrush(DFHomeNovelContentBeans model) {
        if (model == null || !TextUtils.isEmpty(model.getTitle()) && model.getTitle().equals(title)) {
            return;
        }
        title = model.getTitle();
        list = model.getList();
        if (tvTitle != null) {
            tvTitle.setText(model.getTitle());
        }
        setData(list);

    }

    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tvOver.setTextColor(context.getResources().getColor(R.color.text_color_8));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.divider_line_F1F6F9));
        //时间控件
        tvHour.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tvMinute.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tvSecond.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tvSpitOne.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tvSpitTwo.setTextColor(context.getResources().getColor(R.color.text_color_1));
    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tvOver.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));

        tvHour.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tvMinute.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tvSecond.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tvSpitOne.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tvSpitTwo.setTextColor(context.getResources().getColor(R.color.title_text_dark));
    }


}
